#include <iostream>
#include <string>
#include <memory>

// 请求对象
struct LeaveRequest {
    std::string name;  // 请假人
    int days;     // 请假天数
    LeaveRequest(std::string n, int d) : name(n), days(d) {}
};

// 抽象处理者
class Handler {
protected:
    std::shared_ptr<Handler> next;  // 下一个处理者
public:
    virtual ~Handler() = default;

    void setNext(std::shared_ptr<Handler> n) {
        next = n;
    }

    // 处理请求
    virtual void handleRequest(const LeaveRequest &req) {
        if (next) {
            next->handleRequest(req);  // 传递给下一个
        } else {
            std::cout << "无人能处理该请求\n";
        }
    }
};

// 具体处理者 - 组长
class TeamLeader : public Handler {
public:
    void handleRequest(const LeaveRequest &req) override {
        if (req.days <= 2) {
            std::cout << "组长批准 " << req.name << " 请假 " << req.days << " 天\n";
        } else if (next) {
            std::cout << "组长权限不足，转给经理...\n";
            next->handleRequest(req);
        }
    }
};

// 具体处理者 - 经理
class Manager : public Handler {
public:
    void handleRequest(const LeaveRequest &req) override {
        if (req.days <= 5) {
            std::cout << "经理批准 " << req.name << " 请假 " << req.days << " 天\n";
        } else if (next) {
            std::cout << "经理权限不足，转给 CEO...\n";
            next->handleRequest(req);
        }
    }
};

// 具体处理者 - CEO
class CEO : public Handler {
public:
    void handleRequest(const LeaveRequest &req) override {
        std::cout << "CEO 批准 " << req.name << " 请假 " << req.days << " 天\n";
    }
};

int main() {
    // 创建处理者
    auto leader = std::make_shared<TeamLeader>();
    auto manager = std::make_shared<Manager>();
    auto ceo = std::make_shared<CEO>();

    // 组装责任链
    leader->setNext(manager);
    manager->setNext(ceo);

    LeaveRequest r1("张三", 1);
    LeaveRequest r2("李四", 4);
    LeaveRequest r3("王五", 10);

    leader->handleRequest(r1);
    leader->handleRequest(r2);
    leader->handleRequest(r3);

    return 0;
}
